﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task07_GraphGenerator
{
	class Program
	{
		static void Main(string[] args)
		{
            const int GraphNodes = 50;
            int[,] graph = new int[GraphNodes, GraphNodes];
			Random rnd = new Random();

            for (int i = 0; i < GraphNodes; i++)
            {
                int vertexesCount = rnd.Next(1, GraphNodes / 2);
                for (int j = 0; j < vertexesCount; j++)
                {
                    int newVertex = rnd.Next(1, GraphNodes);
                    while (graph[i, newVertex] != 0)
                    {
                        newVertex = rnd.Next(1, GraphNodes);
                    }

                    //int randomWeight = rnd.Next(1, 100);
                    if (i != newVertex)
                    {
                        graph[i, newVertex] = 1;
                    }
                }
            }

            //Console.Write(ConvertToAdjacencyList(graph));

            StringBuilder graphMatrix = new StringBuilder();
            graphMatrix.AppendLine(GraphNodes.ToString());

            for (int i = 0; i < GraphNodes; i++)
            {
                for (int j = 0; j < GraphNodes; j++)
                {
                    graphMatrix.AppendFormat("{0}, ", graph[i, j]);
                }
                graphMatrix.Remove(graphMatrix.Length - 2, 1);
                graphMatrix.AppendLine();
            }

            Console.Write(graphMatrix.ToString());
		}

        static string ConvertToAdjacencyList(int[,] graph)
        {
            StringBuilder adjacensyList = new StringBuilder();

            adjacensyList.AppendLine(graph.GetLength(0).ToString());
            for (int i = 0; i < graph.GetLength(0); i++)
            {
                for (int j = 0; j < graph.GetLength(1); j++)
                {
                    if (graph[i, j] != 0)
                    {
                        adjacensyList.AppendFormat("{0}, {1}, ", j, graph[i, j]);
                    }
                }
                adjacensyList.Remove(adjacensyList.Length - 2, 2);
                adjacensyList.AppendLine();
            }

            return adjacensyList.ToString();
        }

        static int[,] GenerateNotConnected(Random rnd)
        {
            const int GraphNodes = 15000;
            int componentsCount = 500;
            int[,] graph = new int[GraphNodes, GraphNodes];

            int currentComponentStartNode = 0;
            //bool isFinished = false;
            while (currentComponentStartNode < GraphNodes)
            {
                int currentComponentNodesCount = rnd.Next(2, GraphNodes / componentsCount);
                
                for (int i = currentComponentStartNode; i < currentComponentStartNode + currentComponentNodesCount; i++)
                {                        
                    if (currentComponentStartNode + currentComponentNodesCount > GraphNodes)
                    {
                        currentComponentNodesCount = GraphNodes - currentComponentStartNode;
                    }
                    int vertexesCount = rnd.Next(1, currentComponentNodesCount / 2);
                    for (int j = 0; j < vertexesCount; j++)
                    {

                        int newVertex = rnd.Next(currentComponentStartNode, currentComponentStartNode + currentComponentNodesCount);
                        //if (newVertex >= GraphNodes)
                        //{
                        //    isFinished = true;
                        //    break;
                        //}
                        while (graph[i, newVertex] != 0)
                        {
                            newVertex = rnd.Next(currentComponentStartNode, currentComponentStartNode + currentComponentNodesCount);
                        }

                        graph[i, newVertex] = 1;
                    }
                    //if (isFinished)
                    //{
                    //    break;
                    //}
                }
                currentComponentStartNode += currentComponentNodesCount;
            }
            

            return graph;
        }
	}
}
